package cn.wolfcode.service.impl;

import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.CommonCodeMsg;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.fegin.ProductFeginApi;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.redis.CommonRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Qualifier("cn.wolfcode.fegin.ProductFeginApi")
    @Autowired
    private ProductFeginApi productFeginApi;
    
    @Override
    public List<SeckillProductVo> querySeckillProduct() {
        List<SeckillProductVo> ret=new ArrayList();
        //查询秒杀商品表信息
        List<SeckillProduct> seckillProductList = seckillProductMapper.getAll();
        ArrayList<Long> longs = new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProductList) {
            longs.add(seckillProduct.getProductId());
        }
        
        //产品信息
        Result<List<Product>> productList=productFeginApi.queryProductByIds(longs);
      
        if (productList.hasError()|| StringUtils.isEmpty(productList)){
            throw new BusinessException(CommonCodeMsg.NO_SECKILL_PRODUCT);
        }
        //集合转换为map
        HashMap<Long, Product> hashMap = new HashMap<>();
        for (Product datum : productList.getData()) {
            hashMap.put(datum.getId(),datum);
        }
        
        //组装信息
        for (SeckillProduct seckillProduct : seckillProductList) {
            SeckillProductVo vo = new SeckillProductVo();
            Product product = hashMap.get(seckillProduct.getProductId());
            BeanUtils.copyProperties(product,vo);
            BeanUtils.copyProperties(seckillProduct,vo);
            ret.add(vo);
        }

       
        return ret;
    }
    
    @Override
    public List<SeckillProductVo> querySeckillProductByTime(Integer time) {
        String bigkey = CommonRedisKey.SECKILL_PRODUCT.getRealKey(String.valueOf(time));
        Map<Object, Object> map = redisTemplate.opsForHash().entries(bigkey);
        ArrayList<SeckillProductVo> ret = new ArrayList<>();
        for (Object value : map.values()) {
            ret.add(JSON.parseObject(String.valueOf(value),SeckillProductVo.class));
        }
        if(ret.size()<0){
        //    List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
            throw new BusinessException(CommonCodeMsg.NO_PRODUCT);
        }
        return ret;
    }
    
    @Override
    public SeckillProductVo find(Integer time, Integer seckillId) {
        SeckillProductVo ret;
        //从redis中取
        String bigkey = CommonRedisKey.SECKILL_PRODUCT.getRealKey(String.valueOf(time));
        String val = (String) redisTemplate.opsForHash().get(bigkey, String.valueOf(seckillId));
        ret = JSON.parseObject(val, SeckillProductVo.class);
        //如果没有从数据库中取
        if (StringUtils.isEmpty(ret)){
          SeckillProduct seckillProduct = seckillProductMapper.querySeckillOne(seckillId);
          if (StringUtils.isEmpty(seckillProduct)){
              throw new BusinessException(CommonCodeMsg.NO_SECKILL_PRODUCT);
          }
            Result<Product> Result = productFeginApi.queryProductById(seckillProduct.getProductId());
            BeanUtils.copyProperties(Result.getData(),ret);
            BeanUtils.copyProperties(seckillProduct,ret);
        }
        
        return ret;
    }
    
    @Override
    public SeckillProduct findfromDB(Long seckillId) {
        return seckillProductMapper.querySeckillOne(Math.toIntExact(seckillId));
    }
    
    @Override
    public void descContOne(Long seckillId) {
        int stock = seckillProductMapper.decrStock(seckillId);
    }
    
    @Override
    public void incrCountOne(Long seckillId) {
        seckillProductMapper.incrStock(seckillId);
    }
}
